<?php namespace Admin\Controller;

use Hdphp\Controller\Controller;

//测试控制器
class IndexController extends CommonController{
    //动作
    public function index(){
        $aid=$_SESSION['info']['aid'];
        $anickname=Db::table('admin')->where("aid={$aid}")->pluck('anickname');
        View::with('anickname',$anickname);
        View::make();
    }
//    显示欢迎界面
    public function welcome(){
        View::make();
    }

    //    登陆信息
    public function logininfo(){
        $aid=$_SESSION['info']['aid'];
        $anickname=Db::table('admin')->where("aid={$aid}")->pluck('anickname');
        $ip=$_SESSION['info']['aloginip'];
        $lasttime=$_SESSION['info']['alogintime'];
        $lasttime=date('Y-m-d_H:i:s',$lasttime);
        $thistime=Db::table('admin')->where("aid={$aid}")->pluck('alogintime');
        $thistime=date('Y-m-d_H:i:s',$thistime);
        View::with('thistime',$thistime);
        View::with('lasttime',$lasttime);
        View::with('ip',$ip);
        View::with('anickname',$anickname);
        View::make();
    }
//    修改密码
    public function changePassword(){
        if(IS_POST) {
//        1判断新密码是否满足不少于六位的条件
            $newpassword = Q('post.newpassword');
            if (preg_match('/^\w{6,12}+$/', $newpassword) == 0) {
                View::error('请输入6到12位字母或数字下划线');
            }
//        2判断新密码和确认密码是否相等
            $confirmpassword = Q('post.confirmpassword');
            if ($newpassword != $confirmpassword) {
                View::error('新密码两次输入不一致');
            }
//        3判断旧密码是否正确
            $oldpassword=Q('post.oldpassword','','md5');
            $aid=$_SESSION['info']['aid'];
            $dataoldpassword=Db::table('admin')->where("aid='{$aid}'")->pluck('apassword');
            if($oldpassword!=$dataoldpassword){
                View::error('旧密码输入错误');
            }
//        4进行修改密码
            Db::table('admin')->where("aid={$aid}")->update(['apassword'=>md5($newpassword)]);
            session_unset();
            session_destroy();
            echo'<script>parent.location.href="'.U('Login/index').'"</script>';die;
//            echo'<script>parent.location.href="'.U('Login/index').'"</script>';
        }
        View::make();
    }
}
